package com.hung.trade.user.service.Impl;

import com.hung.trade.common.constants.TradeEnums;
import com.hung.trade.common.exception.OrderException;
import com.hung.trade.common.exception.TradeException;
import com.hung.trade.common.protocol.pay.ChangeUserMoneyReq;
import com.hung.trade.common.protocol.pay.ChangeUserMoneyResp;
import com.hung.trade.common.protocol.user.QueryUserReq;
import com.hung.trade.common.protocol.user.QueryUserResp;
import com.hung.trade.dao.mapper.TradeUserMapper;
import com.hung.trade.dao.mapper.TradeUserMoneyLogMapper;
import com.hung.trade.dao.pojo.TradeUser;
import com.hung.trade.dao.pojo.TradeUserMoneyLog;
import com.hung.trade.dao.pojo.TradeUserMoneyLogExample;
import com.hung.trade.user.service.IUserService;
import com.hung.trade.common.exception.UserException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author hungki
 * @createTime 2018-10-08 下午 1:34
 */
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private TradeUserMapper tradeUserMapper;
    @Autowired
    private TradeUserMoneyLogMapper tradeUserMoneyLogMapper;

    @Override
    @Transactional(readOnly = true)
    public QueryUserResp queryUserById(QueryUserReq queryUserReq) {

        QueryUserResp queryUserRes = new QueryUserResp();
        queryUserRes.setRetCode(TradeEnums.RetEnum.SUCCESS.getCode());
        queryUserRes.setRetInfo(TradeEnums.RetEnum.SUCCESS.getDesc());

        try{
            if(null==queryUserReq || null== queryUserReq.getUserId()){
                throw new TradeException("请求参数不正确");
            }
           TradeUser tradeUser= tradeUserMapper.selectByPrimaryKey(queryUserReq.getUserId());
            if(null!=tradeUser){
                BeanUtils.copyProperties(tradeUser,queryUserRes);
            }else{
                throw new TradeException("数据库没有查到对象");
            }
        }catch (TradeException e){
            queryUserRes.setRetCode(TradeEnums.RetEnum.FAIL.getCode());
            queryUserRes.setRetInfo(TradeEnums.RetEnum.FAIL.getDesc());
        }

        return queryUserRes;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ChangeUserMoneyResp changeUserMoney(ChangeUserMoneyReq changeUserMoneyReq) throws UserException {
        System.out.println("changeUserMoneyReq = " + changeUserMoneyReq);
        ChangeUserMoneyResp changeUserMoneyResp = new ChangeUserMoneyResp();
        changeUserMoneyResp.setRetCode(TradeEnums.RetEnum.SUCCESS.getCode());
        changeUserMoneyResp.setRetInfo(TradeEnums.RetEnum.SUCCESS.getDesc());

        try{
            if(null==changeUserMoneyReq || null== changeUserMoneyReq.getUserId()){
                throw new TradeException("请求参数不正确");
            }

            TradeUserMoneyLog tradeUserMoneyLog = new TradeUserMoneyLog();
            tradeUserMoneyLog.setUserId(changeUserMoneyReq.getUserId());
            tradeUserMoneyLog.setOrderId(changeUserMoneyReq.getOrderId());
            tradeUserMoneyLog.setCreateTime(new Date());
            tradeUserMoneyLog.setUseMoney(changeUserMoneyReq.getUserMoney());
            tradeUserMoneyLog.setMoneyLogType(changeUserMoneyReq.getMoneyLogType());
            //int j = tradeUserMoneyLogMapper.insert(tradeUserMoneyLog);
            //if(j==0){
            //    throw new TradeException("保存用户修改余额记录异常");
            //}


            TradeUser tradeUser = new TradeUser();
            tradeUser.setUserId(changeUserMoneyReq.getUserId());
            tradeUser.setUserMoney(changeUserMoneyReq.getUserMoney());

            if(changeUserMoneyReq.getMoneyLogType().equals(TradeEnums.UserMoneyLogTypeEnum.PAID.getCode())){
                TradeUserMoneyLogExample logExample = new TradeUserMoneyLogExample();
                //查询付款记录
                logExample.createCriteria().andUserIdEqualTo(changeUserMoneyReq.getUserId())
                        .andOrderIdEqualTo(changeUserMoneyReq.getOrderId())
                        .andMoneyLogTypeEqualTo(TradeEnums.UserMoneyLogTypeEnum.PAID.getCode());
                int count = tradeUserMoneyLogMapper.countByExample(logExample);
                if(count>0){
                    throw  new OrderException("已经付款，不能再次付款");
                }
                int k = tradeUserMapper.reduceUserMoney(tradeUser);
                if(k==0){
                    throw new TradeException("减少用户余额异常");
                }
            }
            if(changeUserMoneyReq.getMoneyLogType().equals(TradeEnums.UserMoneyLogTypeEnum.RETURNED.getCode())){
                TradeUserMoneyLogExample logExample = new TradeUserMoneyLogExample();

                //查询付款记录
                logExample.createCriteria().andUserIdEqualTo(changeUserMoneyReq.getUserId())
                        .andOrderIdEqualTo(changeUserMoneyReq.getOrderId())
                        .andMoneyLogTypeEqualTo(TradeEnums.UserMoneyLogTypeEnum.PAID.getCode());
                int count = tradeUserMoneyLogMapper.countByExample(logExample);
                if(count==0){
                    throw  new OrderException("没有下单记录,不能发起退款");
                }
                //避免多次退款
                logExample.createCriteria().andUserIdEqualTo(changeUserMoneyReq.getUserId())
                        .andOrderIdEqualTo(changeUserMoneyReq.getOrderId())
                        .andMoneyLogTypeEqualTo(TradeEnums.UserMoneyLogTypeEnum.RETURNED.getCode());
                count =tradeUserMoneyLogMapper.countByExample(logExample);
                if(count==0){
                    throw  new OrderException("已经退款，不能重复退款");
                }

                int k = tradeUserMapper.addUserMoney(tradeUser);
                if(k==0){
                    throw new TradeException("增加用户余额异常");
                }
                count = tradeUserMoneyLogMapper.insert(tradeUserMoneyLog);
                if(k==0){
                    throw new TradeException("退款时，保存退款记录异常");
                }
            }

        }catch (Exception e){
            e.printStackTrace();
            changeUserMoneyResp.setRetCode(TradeEnums.RetEnum.FAIL.getCode());
            changeUserMoneyResp.setRetInfo(TradeEnums.RetEnum.FAIL.getDesc()+e.getMessage());
            throw new UserException(e);
        }

        return changeUserMoneyResp;
    }
}

